/*
    Copyright 2009 Spencer B. Koehler, Ph.D.

    This file is part of the Brillig Toolkit.

    The Brillig Toolkit is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Brillig Toolkit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with The Brillig Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.brillig.extract;


import org.brillig.nlp.BreakStrategy;
import org.brillig.nlp.StringWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * A regex-based text splitter.
 * <p>
 * @author Spence Koehler
 */
public class RegexSplitter implements TextSplitter {
  
  private String pattern;

  public RegexSplitter(String pattern) {
    this.pattern = pattern;
  }

  /**
   * Split the text into chunks if applicable.
   *
   * @param docText  The docText to split.
   *
   * @return split pieces of text or null if text is not applicable.
   */
  public StringWrapper[] split(DocText docText, BreakStrategy breakStrategy) {
    final String string = docText.getString();
    final String[] pieces = string.split(pattern);

    final List<StringWrapper> result = new ArrayList<StringWrapper>();

    for (int i = 0; i < pieces.length; ++i) {
      if (!"".equals(pieces[i])) {
        result.add(new StringWrapper(pieces[i], breakStrategy));
      }
    }

    return result.toArray(new StringWrapper[result.size()]);
  }
}
